﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace Zipun.VkOnlineMusic.View.WinForms.Controls
{
    public partial class VomVScrollBar : UserControl
    {
        public event EventHandler<EventArgs> PositionChanged;
        private int _buttonHeight;
        [Browsable(false)]
        public double Position
        {
            get
            {
                return (double)(sliderPictureBox.Top - _buttonHeight) / (Height - 2 * _buttonHeight - sliderPictureBox.Height);
            }
            set
            {
                var newPosition = (int)((Height - 2 * _buttonHeight - sliderPictureBox.Height) * value + _buttonHeight);
                sliderPictureBox.Top = correctPosition(newPosition);
            }
        }

        [Browsable(false)]
        public double SliderSize
        {
            get { return (double)sliderPictureBox.Height / (Height - 2 * _buttonHeight); }
            set
            {
                var newSize = (int)((Height - 2 * _buttonHeight) * value);
                if (newSize < 16)
                    newSize = 16;
                if (newSize > Height - 2 * _buttonHeight)
                    newSize = Height - 2 * _buttonHeight;
                sliderPictureBox.Height = newSize;
            }
        }

        private bool _isSlide;

        public VomVScrollBar()
        {
            InitializeComponent();
        }

        public int AutohideDelay
        {
            get { return autoHideTimer.Interval; }
            set { autoHideTimer.Interval = value; }
        }

        public int HideSpeed
        {
            get { return animationTimer.Interval; }
            set { animationTimer.Interval = value; }
        }

        public int ScrollSpeed
        {
            get { return scrollTimer.Interval; }
            set { scrollTimer.Interval = value; }
        }

        public void StartAutoHide()
        {
            autoHideTimer.Enabled = true;
        }

        public void AutoShow()
        {
            autoHideTimer.Enabled = false;
            _isHiding = false;
            animationTimer.Enabled = true;

        }

        private void startSlide(object sender, MouseEventArgs e)
        {
            _isSlide = true;
        }

        private void stopSlide(object sender, MouseEventArgs e)
        {
            _isSlide = false;
            scrollTimer.Enabled = false;
        }

        private void sliderPictureBox_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left)
                _isSlide = false;
            if (!_isSlide)
                return;
            var control = sender as Control;
            if (control == null)
                return;
            var newPosition = PointToClient(control.PointToScreen(new Point(e.X, e.Y))).Y - sliderPictureBox.Height / 2;
            sliderPictureBox.Top = correctPosition(newPosition);
            if (PositionChanged != null)
                PositionChanged.Invoke(this, new EventArgs());
        }

        private int correctPosition(int newPosition)
        {
            if (newPosition < upPictureBox.Height)
                newPosition = upPictureBox.Height;
            if (newPosition + sliderPictureBox.Height > downPictureBox.Top)
                newPosition = downPictureBox.Top - sliderPictureBox.Height;
            return newPosition;
        }

        private void VomVScrollBar_MouseLeave(object sender, EventArgs e)
        {
            _isSlide = false;
            scrollTimer.Enabled = false;
            StartAutoHide();
        }

        private void VomVScrollBar_Load(object sender, EventArgs e)
        {
            _buttonHeight = upPictureBox.Height;
            DoubleBuffered = true;
        }

        private void upPictureBox_Click(object sender, EventArgs e)
        {
            var newPosition = Position - SliderSize * .1;
            if (newPosition < 0)
                newPosition = 0;
            Position = newPosition;
            if (PositionChanged != null)
                PositionChanged.Invoke(this, new EventArgs());
        }

        private void downPictureBox_Click(object sender, EventArgs e)
        {
            var newPosition = Position + SliderSize * .1;
            if (newPosition > 1)
                newPosition = 1;
            Position = newPosition;
            if (PositionChanged != null)
                PositionChanged.Invoke(this, new EventArgs());
        }

        private void autoHideTimer_Tick(object sender, EventArgs e)
        {
            autoHideTimer.Enabled = false;
            autoHide();
        }

        private void autoHide()
        {
            MinimumSize = new Size(0, 0);
            _isHiding = true;
            animationTimer.Enabled = true;
        }

        private bool _isHiding;
        private void animationTimer_Tick(object sender, EventArgs e)
        {
            var newWidth = Width + 5 * (_isHiding ? -1 : 1);
            if (newWidth < 0)
            {
                newWidth = 0;
                animationTimer.Enabled = false;
            }
            if (newWidth > 19)
            {
                newWidth = 19;
                animationTimer.Enabled = false;
            }
            Width = newWidth;
        }

        private bool _isScrollUp;
        private void VomVScrollBar_MouseDown(object sender, MouseEventArgs e)
        {
            scrollTimer.Enabled = true;
            _isScrollUp = e.Y <= sliderPictureBox.Top;
        }

        private void scrollTimer_Tick(object sender, EventArgs e)
        {
            var newPosition = Position + SliderSize * (_isScrollUp ? -1 : 1);
            if (newPosition > 1)
            {
                newPosition = 1;
                scrollTimer.Enabled = false;
            }
            if (newPosition < 0)
            {
                newPosition = 0;
                scrollTimer.Enabled = false;
            }
            Position = newPosition;
            if (PositionChanged != null)
                PositionChanged(this, e);
        }

        private void VomVScrollBar_MouseEnter(object sender, EventArgs e)
        {
            AutoShow();
        }

        private void upPictureBox_MouseEnter(object sender, EventArgs e)
        {
            AutoShow();
        }

        private void upPictureBox_MouseLeave(object sender, EventArgs e)
        {
            StartAutoHide();
        }
    }
}
